﻿using UnityEngine;
using System.Collections;

public class PlaneMove : MonoBehaviour {
    public float speed = 5;
    public static Vector2 MaxSize;
	void Awake () {
        MaxSize = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
	}
   
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            pos.x = Mathf.Clamp(pos.x, -MaxSize.x, MaxSize.x);
            pos.y = Mathf.Clamp(pos.y, -MaxSize.y, MaxSize.y);
            //计算飞机到目标的向量
            Vector2 offset = pos - (Vector2)transform.position;
            if (offset.sqrMagnitude>0.01f)
            {
                Vector2 dir = offset.normalized;
                transform.Translate(dir * speed * Time.deltaTime);
            }
        } 
    }
}
